﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using MM.DBUtility;

namespace MobileManager
{
    public partial class RTsimprovince : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                BindDdlCustomer(ref ddlCustomer);
                ddlCustomer.Items.Insert(0, new ListItem("全部", "-1"));

                BindDdlODEP(ref ddlODEP);
                ddlODEP.Items.Insert(0, new ListItem("全部", "-1"));

                inputDateFrom.Value = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

                GVDataBind();
            }
        }
        private void BindDdlCustomer(ref DropDownList ddl)
        {
            string strSQL = "SELECT CNO,CNO+'-'+ CName as Name FROM T_Customers WITH(NOLOCK)";
            if (!string.IsNullOrEmpty(userModel.CustomerNO))
                strSQL += string.Format("WHERE CNO IN ({0})", BLSqlData.GetInSQL(userModel.CustomerNO));
            ddl.DataSource = DbHelperSql.Query(strSQL).Tables[0];
            ddl.DataValueField = "CNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void BindDdlODEP(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ONO,ONO+'-'+ OName as Name FROM T_ODEP WITH(NOLOCK)").Tables[0];
            ddl.DataValueField = "ONO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "请求时间From必须小于等于To！");
                        return;
                    }
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "请求时间From必须小于等于To！");
                        return;
                    }
                }
                else
                {
                    MM.Common.Common.PageStartAlert(this.Page, "时间From,To必须填写！");
                    return;
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = string.Empty, strSQLF = string.Empty;
            if ("0".Equals(ddlByType.SelectedValue))
            {
                strSQLF = "SELECT DISTINCT c.ProvinceName AS InfoType,a.IMSI FROM T_SIMall1 a,T_Area b,T_Province c WITH(NOLOCK) WHERE a.AreaNO=b.AreaNO and a.ONO=b.ONO and b.ProvinceID=c.ProvinceID ";
                string strProvinceID = MM.DBUtility.BLSqlData.GetProvinceIDbyQuickName(txtProvince.TextValue);
                if (!string.IsNullOrEmpty(strProvinceID))
                    strSQLF += string.Format(" AND c.ProvinceID={0}", strProvinceID);
                if (!"-1".Equals(ddlCustomer.SelectedValue))
                    strSQLF += string.Format(" AND a.CNO='{0}'", ddlCustomer.SelectedValue);
                if (!"-1".Equals(ddlODEP.SelectedValue))
                    strSQLF += string.Format(" AND a.ONO='{0}'", ddlODEP.SelectedValue);
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                    strSQLF += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));
            }
            else if ("1".Equals(ddlByType.SelectedValue))
            {
                strSQLF = "SELECT DISTINCT a.CNO AS InfoType,a.IMSI FROM T_SIMall1 a,T_Area b,T_Province c WITH(NOLOCK) WHERE a.AreaNO=b.AreaNO and a.ONO=b.ONO and b.ProvinceID=c.ProvinceID ";
                string strProvinceID = MM.DBUtility.BLSqlData.GetProvinceIDbyQuickName(txtProvince.TextValue);
                if (!string.IsNullOrEmpty(strProvinceID))
                    strSQLF += string.Format(" AND c.ProvinceID={0}", strProvinceID);
                if (!"-1".Equals(ddlCustomer.SelectedValue))
                    strSQLF += string.Format(" AND a.CNO='{0}'", ddlCustomer.SelectedValue);
                if (!"-1".Equals(ddlODEP.SelectedValue))
                    strSQLF += string.Format(" AND a.ONO='{0}'", ddlODEP.SelectedValue);
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                    strSQLF += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));
            }

            strSQL = string.Format("SELECT * FROM (SELECT InfoType,COUNT(IMSI) AS AllNum FROM ({0}) AS A GROUP BY InfoType) AS B ORDER BY AllNum DESC", strSQLF);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

            object o = DbHelperSql.GetSingle(string.Format("SELECT COUNT(*) FROM ({0}) AS B", strSQLF));
            if (o != null)
            {
                lbRnumAll.Visible = true;
                lbRnumAll.Text = "总IMSI数：" + o.ToString();
            }
            else lbRnumAll.Visible = false;
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Label lbInfo = (Label)e.Row.FindControl("lbByType");
                if ("0".Equals(ddlByType.SelectedValue))
                    lbInfo.Text = "省份";
                else if ("1".Equals(ddlByType.SelectedValue))
                    lbInfo.Text = "客户编号";
            }
        }
        #endregion
    }
}
